import prismaDb from "../../utils/prismadb";
import {hash} from "bcrypt";
import { NextResponse } from "next/server";

export const POST = async (req:Request)=>{
    if(req.method !== "POST"){
        return NextResponse.json({ message: "Method not allowed" }, { status: 405 })
    }
    try {
        const {email,name,password} = await req.json();
        const existingUser = await prismaDb.user.findUnique({
            where: {
                email: email,
            }
        })
        if(existingUser){
            return NextResponse.json({ message: "User already exists" }, { status: 400 })
        }
        const hashedPassword = await hash(password,12)
        const user = await prismaDb.user.create({
            data: {
                email,
                name,
                hashedPassword,
                image:'',
                emailVerified:new Date()
            }
        })
        return  NextResponse.json({ data: user }, { status: 200 })
    } catch (error) {
        console.log(error)
        return NextResponse.json({ message:"Internal server error" }, { status: 400 })
    }

}


// export const POST = async (req:Request)=>{
//     /**
//      * @param {在终端输出info}
//      */
//     const info = await req.json();
//     console.log(info);
//     return NextResponse.json({
//         success:true,
//         errorMessage:"",
//         data:"新增成功"
//     })
// }



